package iamutkarshtiwari.github.io.ananas.editimage.fliter;


import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

public class PhotoProcessing {
    private static final String TAG = "PhotoProcessing";

    /**
     * 滤镜处理
     *
     * @param bitmap   PixelMap对象
     * @param position 滤镜index
     * @return 效果图
     */
    public static PixelMap filterPhoto(PixelMap bitmap, int position) {
        if (bitmap != null) {
            sendBitmapToNative(bitmap);
        }
        switch (position) {
            case 0: // Original
                break;
            case 1: // Instafix
                nativeApplyInstafix();
                break;
            case 2: // Ansel
                nativeApplyAnsel();
                break;
            case 3: // Testino
                nativeApplyTestino();
                break;
            case 4: // XPro
                nativeApplyXPro();
                break;
            case 5: // Retro
                nativeApplyRetro();
                break;
            case 6: // Black & White
                nativeApplyBW();
                break;
            case 7: // Sepia
                nativeApplySepia();
                break;
            case 8: // Cyano
                nativeApplyCyano();
                break;
            case 9: // Georgia
                nativeApplyGeorgia();
                break;
            case 10: // Sahara
                nativeApplySahara();
                break;
            case 11: // HDR
                nativeApplyHDR();
                break;
        }
        PixelMap filteredBitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
        return filteredBitmap;
    }

    // /////////////////////////////////////////////
    static {
        System.loadLibrary("photoprocessing");
    }

    public static native int nativeInitBitmap(int width, int height);

    public static native void nativeGetBitmapRow(int y, int[] pixels);

    public static native void nativeSetBitmapRow(int y, int[] pixels);

    public static native int nativeGetBitmapWidth();

    public static native int nativeGetBitmapHeight();

    public static native void nativeDeleteBitmap();

    public static native int nativeRotate90();

    public static native void nativeRotate180();

    public static native void nativeFlipHorizontally();

    public static native void nativeApplyInstafix();

    public static native void nativeApplyAnsel();

    public static native void nativeApplyTestino();

    public static native void nativeApplyXPro();

    public static native void nativeApplyRetro();

    public static native void nativeApplyBW();

    public static native void nativeApplySepia();

    public static native void nativeApplyCyano();

    public static native void nativeApplyGeorgia();

    public static native void nativeApplySahara();

    public static native void nativeApplyHDR();

    public static native void nativeLoadResizedJpegBitmap(byte[] jpegData,int size, int maxPixels);

    public static native void nativeResizeBitmap(int newWidth, int newHeight);

    public static native void handleSmooth(PixelMap bitmap, float smoothValue);

    public static native void handleWhiteSkin(PixelMap bitmap, float whiteValue);

    public static native void handleSmoothAndWhiteSkin(PixelMap bitmap, float smoothValue, float whiteValue);

    public static native void freeBeautifyMatrix();

    private static void sendBitmapToNative(PixelMap bitmap) {
//        int width = bitmap.getWidth();
//        int height = bitmap.getHeight();
        Size size = bitmap.getImageInfo().size;
        int width = size.width;
        int height = size.height;

        Rect region;
        nativeInitBitmap(width, height);
        int[] pixels = new int[width];
        for (int y = 0; y < height; y++) {
//            bitmap.getPixels(pixels, 0, width, 0, y, width, 1);
            region = new Rect(0, y, width, 1);
            bitmap.readPixels(pixels, 0, width, region);
            nativeSetBitmapRow(y, pixels);
        }
    }

    private static PixelMap getBitmapFromNative(PixelMap bitmap) {
        if (bitmap == null) return null;
        int width = nativeGetBitmapWidth();
        int height = nativeGetBitmapHeight();
        Size size = bitmap.getImageInfo().size;

        if (width != size.width || height != size.height || !bitmap.isEditable()) { // in
//            Config config = Config.ARGB_8888;
            PixelMap.InitializationOptions config = new PixelMap.InitializationOptions();
            config.pixelFormat = PixelFormat.ARGB_8888;
            config.size = size;
            //                config = bitmap.getConfig();
            config.pixelFormat = bitmap.getImageInfo().pixelFormat;
            bitmap.release();
            //            bitmap = Bitmap.createBitmap(width, height, config);
            bitmap = PixelMap.create(config);
        }

        int[] pixels = new int[width];
        Rect region;
        for (int y = 0; y < height; y++) {
            nativeGetBitmapRow(y, pixels);
//            bitmap.setPixels(pixels, 0, width, 0, y, width, 1);
            region = new Rect(0, y, width, 1);
            bitmap.writePixels(pixels, 0, width, region);
        }

        return bitmap;
    }

    /**
     * 返回可变的图片 将editable属性置为true
     *
     * @param bitmap 原图
     * @return 新图片
     */
    public static PixelMap makeBitmapMutable(PixelMap bitmap) {
        sendBitmapToNative(bitmap);
        return getBitmapFromNative(bitmap);
    }

    /**
     * 图片旋转
     *
     * @param bitmap 原图
     * @param angle  角度
     * @return 旋转后的图片
     */
    public static PixelMap rotate(PixelMap bitmap, int angle) {
//        int width = bitmap.getWidth();
//        int height = bitmap.getHeight();
//        Config config = bitmap.getConfig();
        ImageInfo info = bitmap.getImageInfo();
        int width = info.size.width;
        int height = info.size.height;
        PixelMap.InitializationOptions config = new PixelMap.InitializationOptions();
        config.pixelFormat = info.pixelFormat;
        config.size = info.size;

        nativeInitBitmap(width, height);
        sendBitmapToNative(bitmap);
        if (angle == 90) {
            nativeRotate90();
//            bitmap.recycle();
//            bitmap = Bitmap.createBitmap(height, width, config);
            bitmap.release();
            bitmap = PixelMap.create(config);
            bitmap = getBitmapFromNative(bitmap);
            nativeDeleteBitmap();
        } else if (angle == 180) {
            nativeRotate180();
//            bitmap.recycle();
//            bitmap = Bitmap.createBitmap(width, height, config);
            bitmap.release();
            bitmap = PixelMap.create(config);
            bitmap = getBitmapFromNative(bitmap);
            nativeDeleteBitmap();
        } else if (angle == 270) {
            nativeRotate180();
            nativeRotate90();
//            bitmap.recycle();
//            bitmap = Bitmap.createBitmap(height, width, config);
            bitmap.release();
            bitmap = PixelMap.create(config);
            bitmap = getBitmapFromNative(bitmap);
            nativeDeleteBitmap();
        }
        return bitmap;
    }

    /**
     * 水平翻转
     *
     * @param bitmap 原图
     * @return 效果图
     */
    public static PixelMap flipHorizontally(PixelMap bitmap) {
//        nativeInitBitmap(bitmap.getWidth(), bitmap.getHeight());
        nativeInitBitmap(bitmap.getImageInfo().size.width, bitmap.getImageInfo().size.height);
        sendBitmapToNative(bitmap);
        nativeFlipHorizontally();
        bitmap = getBitmapFromNative(bitmap);
        nativeDeleteBitmap();
        return bitmap;
    }
}
